//
//  AccessoryView.m
//  Village
//
//  Created by longrise on 2017/9/14.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "AccessoryView.h"
#import "LCDownloadManager.h"
#import "ProgressView.h"

@interface AccessoryView()
@property (nonatomic, strong) ProgressView *prossView;
@end

@implementation AccessoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
  
        _Accimage = [[UIImageView alloc] init];
        _Accimage.image = [UIImage imageNamed:@"accessory"];
        [self addSubview:_Accimage];
        [_Accimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(22*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.top.equalTo(self.mas_top).offset(10*kScreenScaleY);
            make.width.mas_equalTo(19*kScreenScaleX);
            make.height.mas_equalTo(21*kScreenScaleY);
            
        }];
        
        self.Acclable = [[UILabel alloc]init];
        self.Acclable.text = @"《附件1》";
        self.Acclable.font = [UIFont systemFontOfSize:16*kScreenScaleX];
        [ self.Acclable setTextAlignment : NSTextAlignmentLeft] ;
        
        [self addSubview:self.Acclable];
        
        [self.Acclable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_Accimage.mas_right).offset(7*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.width.mas_equalTo(100*kScreenScaleX);
            make.height.mas_equalTo(21*kScreenScaleY);
            make.top.equalTo(self.mas_top).offset(10*kScreenScaleY);
        }];

        
        _Accbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_Accbutton setImage:[UIImage imageNamed:@"accessory2"] forState:UIControlStateNormal];
        _Accbutton.tag = 1;
        [_Accbutton addTarget:self action:@selector(accbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_Accbutton];
        [_Accbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-20*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.width.mas_equalTo(19*kScreenScaleX);
            make.height.mas_equalTo(19*kScreenScaleY);
            make.top.equalTo(self.mas_top).offset(10*kScreenScaleY);
        }];
        

        _Accimage1 = [[UIImageView alloc] init];
        _Accimage1.image = [UIImage imageNamed:@"accessory1"];
        [self addSubview:_Accimage1];
        [_Accimage1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(22*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.top.equalTo(self.mas_top).offset(50*kScreenScaleY);
            make.width.mas_equalTo(19*kScreenScaleX);
            make.height.mas_equalTo(21*kScreenScaleY);
            
        }];
        
        self.Acclable1 = [[UILabel alloc]init];
        self.Acclable1.text = @"《附件2》";
        self.Acclable1.font = [UIFont systemFontOfSize:16*kScreenScaleX];
        [ self.Acclable1 setTextAlignment : NSTextAlignmentLeft] ;
        
        [self addSubview:self.Acclable1];
        
        [self.Acclable1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_Accimage1.mas_right).offset(7*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.width.mas_equalTo(100*kScreenScaleX);
            make.height.mas_equalTo(21*kScreenScaleY);
            make.top.equalTo(self.mas_top).offset(50*kScreenScaleY);
        }];

        _Accbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_Accbutton1 setImage:[UIImage imageNamed:@"accessory2"] forState:UIControlStateNormal];
        _Accbutton1.tag = 2;
        [_Accbutton1 addTarget:self action:@selector(accbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_Accbutton1];
        [_Accbutton1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-20*kScreenScaleX);//使左边等于self.view的左边，间距为0
            make.width.mas_equalTo(19*kScreenScaleX);
            make.height.mas_equalTo(19*kScreenScaleY);
            make.top.equalTo(self.mas_top).offset(50*kScreenScaleY);
        }];



    }
    return self;
}

- (void)setModel:(PolicyModel *)model
{
    _model = model;
}

- (void)accbuttonClick:(UIButton *)btn
{
    self.prossView = [[ProgressView alloc] init];
    [self.prossView show];
    
    NSArray *array = [Tools toJSONData:self.model.fj];
    NSLog(@"附件 ------  %@",array);
    NSDictionary *dict = [array firstObject];
    
    NSMutableArray *fileAry = [NSMutableArray array];
    NSMutableArray *nameAry = [NSMutableArray array];
    [fileAry addObject:dict[@"httpurl"]];
    [nameAry addObject:[NSString stringWithFormat:@"fj_name_%ld",btn.tag]];
    [[LCDownloadManager getInstanceWithuserflag:[NSString stringWithFormat:@"%@_%ld",self.model.ID,btn.tag] withStoragePath:[NSString stringWithFormat:@"fj_%ld",btn.tag]] downloadFileWith:fileAry withNameArr:nameAry progressbloack:^(CGFloat progress, long long totalMBRead, long long totalMBExpectedToRead) {
        self.prossView.prossCount = progress;
    } successBlock:^(NSString *sucDownPath, NSString *SucDownName) {
        [self.prossView hide];
        
        [btn setImage:[UIImage imageNamed:@"down_on"] forState:UIControlStateNormal];
        btn.enabled = NO;
    } failureBlock:^(NSError *error, NSString *errorDownPath, NSString *errorDowName) {
        
    }];
    
}

@end
